<?php
/**
*
* 日    期：2016-09-20
* 版    本：1.0.0
* 功能说明：客户控制器。
*
**/

namespace Admin\Controller;
use Admin\Controller\ComController;
use Vendor\Tree;

class CustomerController extends ComController {

	public function add(){
		
		$ctype = M('ctype')->field('id,pid,name')->order('o asc')->select();
		$tree = new Tree($ctype);
		$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
		$ctype = $tree->get_tree(0,$str,0);
		$this->assign('ctype',$ctype);//导航
		$this -> display();
	}
		
	public function index($sid=0,$p=1){
		
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;
		$UID=session("UID");
		$customer = M('customer');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		$prefix = C('DB_PREFIX');
		if($sid){
//			$where = "{$prefix}customer.sid=$sid,{$prefix}customer.uid=$UID";
			$where=array(
				"{$prefix}customer.sid"=>1,
				"{$prefix}customer.uid"=>$UID
			);
		}else{
			$where = "{$prefix}customer.uid=$UID";
		}
		$count = $customer->where($where)->count();
		$list  = $customer->field("{$prefix}customer.*,{$prefix}ctype.name")->where($where)->order("{$prefix}customer.cid desc")->join("{$prefix}ctype ON {$prefix}ctype.id = {$prefix}customer.sid")->limit($offset.','.$pagesize)->select();
		
		$page	=	new \Think\Page($count,$pagesize); 
		$page = $page->show();
        $this->assign('list',$list);	
        $this->assign('page',$page);
		$this -> display();
	}


	public function gx($sid=0,$p=1){
		$sid = intval($sid);
		$p = intval($p)>0?$p:1;

		$customer = M('customer');
		$pagesize = 20;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		$prefix = C('DB_PREFIX');
		if($sid){
//			$where = "{$prefix}customer.sid=$sid,{$prefix}customer.uid=$UID";
			$where=array(
				"{$prefix}customer.sid"=>1,
				"{$prefix}customer.uid"=>0,
				"{$prefix}customer.did"=>0,
			);
		}else{
//			$where = "{$prefix}customer.uid=$UID";
			$where=array(
				"{$prefix}customer.uid"=>0,
				"{$prefix}customer.did"=>0,
			);
		}
		$count = $customer->where($where)->count();
		$list  = $customer->field("{$prefix}customer.*,{$prefix}ctype.name")->where($where)->order("{$prefix}customer.cid desc")->join("{$prefix}ctype ON {$prefix}ctype.id = {$prefix}customer.sid")->limit($offset.','.$pagesize)->select();

		$page	=	new \Think\Page($count,$pagesize);
		$page = $page->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this -> display();
	}
	
	public function upgx(){
		$cid=I("get.cid");
		if (isset($cid)){
			$data['uid']=0;
			$data['did']=0;
			M('customer')->data($data)->where('cid='.$cid)->save();
			addlog('共享客户，CID：'.$cid);
			$this->success('恭喜！共享客户成功！');
		}
	}

	public function del(){
		$ids = isset($_POST['ids'])?$_POST['ids']:false;
		if(is_array($ids)){
//			foreach($uids as $k=>$v){
//				$uids[$k] = intval($v);
//			}
			$ids = implode(',',$ids);
			$map['id']  = array('in',$ids);
			if(M('auth_group')->where($map)->delete()){
				addlog('删除用户组ID：'.$ids);
				$this->success('恭喜，用户组删除成功！');
			}else{
				$this->error('参数错误！');
			}
		}else{
			$this->error('参数错误！');
		}
	}
	
	public function edit($cid){
		
		$cid = intval($cid);
		$customer = M('customer')->where('cid='.$cid)->find();
		if($customer){
			
			$ctype = M('ctype')->field('id,pid,name')->order('o asc')->select();
			$tree = new Tree($ctype);
			$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
			$ctype = $tree->get_tree(0,$str,$customer['sid']);
			$this->assign('ctype',$ctype);//导航
			
			$this->assign('customer',$customer);
		}else{
			$this->error('参数错误！');
		}
		$this -> display();
	}
	
	public function update($cid=0){
		
		$cid = intval($cid);
		$data['sid'] = isset($_POST['sid'])?intval($_POST['sid']):0;
		$data['uid']=session("UID");
		$data['did']=session("DID");
		$data['cust_name'] = isset($_POST['cust_name'])?$_POST['cust_name']:false;
		$data['cust_licence_no'] = isset($_POST['cust_licence_no'])?$_POST['cust_licence_no']:false;
		$data['cust_addr'] = isset($_POST['cust_addr'])?$_POST['cust_addr']:false;
		$data['cust_region'] = isset($_POST['cust_region'])?$_POST['cust_region']:false;
		$data['cust_tel'] = isset($_POST['cust_tel'])?$_POST['cust_tel']:false;
		if(!$data['sid'] or !$data['cust_name'] or !$data['cust_region']){
			$this->error('警告！客户分类、客户标题及公司地址为必填项目。');
		}
		if($cid){
			M('customer')->data($data)->where('cid='.$cid)->save();
			addlog('编辑客户，CID：'.$cid);
			$this->success('恭喜！客户编辑成功！');
		}else{
			$cid = M('customer')->data($data)->add();
			if($cid){
				addlog('新增客户，CID：'.$cid);
				$this->success('恭喜！客户新增成功！');
			}else{
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
}